﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Model;
using EQT.Dal;
using EQT.Bll;

public partial class TaskAdmin : System.Web.UI.Page
{
    private DaDept dalDept = new DaDept(DaCommon.DefaultDb);
    private DaEmployee daemp = new DaEmployee(DaCommon.DefaultDb);
    private DaReportAudit dara = new DaReportAudit(DaCommon.DefaultDb);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //绑定合同状态下拉列表
            Utility.BindListCtl2Dic(DropDownListStatus, ContractStatusCollection.TesterGrpStatus);
            InitDefaultFitler();
            LoadTaskList();
        }
    }

    private void InitDefaultFitler()
    {
        DateTime default_begin_day = new DateTime(DateTime.Today.Year, 1, 1);
        TextBoxBegin.Text = default_begin_day.ToString(SiteConfig.DateFormat);
        TextBoxEnd.Text = DateTime.Now.AddDays(1).ToString(SiteConfig.DateFormat);
        DropDownListStatus.SelectedIndex = DropDownListStatus.Items.IndexOf(DropDownListStatus.Items.FindByValue(ContractStatus.ALL.ToString()));
        TextBoxReportId.Text = String.Empty;
        TextBoxCorpName.Text = String.Empty;
    }

    /// <summary>
    /// 加载任务列表
    /// </summary>
    private void LoadTaskList()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        //时间范围是必须的
        sb.Append(DaCoTestItem.SQL_GET_CO_TEST_ITEM);
        sb.Append(" WHERE ");
        sb.Append(Utility.GenerateQueryFilter2DateTime("Report_Create_Time", TextBoxBegin.Text, TextBoxEnd.Text));
        string filter = Utility.GenerateQueryFilterString("Report_ID", TextBoxReportId.Text, 2);
        if (filter != null) sb.AppendFormat(" AND {0} ", filter);
        filter = Utility.GenerateQueryFilterString("Corp_Name", TextBoxCorpName.Text, 2);
        if (filter != null) sb.AppendFormat(" AND {0} ", filter);

        sb.AppendFormat(" AND {0}", Utility.GenerateQueryFilterIntMaskAnd("Report_Status", "&", Int32.Parse(DropDownListStatus.SelectedValue)));
        string sql = sb.ToString();


        if (Utility.GlobalLog.IsDebugEnabled) Utility.GlobalLog.Debug(sql);
        int totalCount = 0;
        DataSet ds = DaCommon.GetPaginationData(sql,
            SiteConfig.PageSize, AspNetPager1.CurrentPageIndex - 1, "Report_Create_Time DESC", out totalCount);
        AspNetPager1.PageSize = SiteConfig.PageSize;
        AspNetPager1.RecordCount = totalCount;
        GridViewTestItems.DataSource = ds;
        GridViewTestItems.DataBind();
    }


    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        LoadTaskList();
    }
    protected void btnFilterQuery_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 0;
        LoadTaskList();
    }
    protected void btnQueryAll_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 0;
        InitDefaultFitler();
        LoadTaskList();
    }

    /// <summary>
    /// 下发一个任务.下发一个任务是弹出本组成员，选择人员下发
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnAssignCoTestItem_Click(object sender, EventArgs e)
    {
        //刷新列表
        LoadTaskList();
    }
   
     
    /// <summary>
    /// 绑定待检验项目时，为【分派人员】按钮添加弹出窗口的脚本。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridViewTestItems_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button BtnAssignCoTestItem = e.Row.FindControl("BtnAssignCoTestItem") as Button;
            if (BtnAssignCoTestItem != null && BtnAssignCoTestItem.Visible)
            {
                DataRowView drv = e.Row.DataItem as DataRowView;
                int state = (int)drv["Test_Status"];
                BtnAssignCoTestItem.Visible = !(state >= (int)CoTestItemStatus.TestDone && state < (int)CoTestItemStatus.Untread);
                if(BtnAssignCoTestItem.Visible)
                BtnAssignCoTestItem.Attributes.Add("OnClick", "InvokePopSelectTester('" + drv["Item_Code"] + "','" + drv["Co_Item_ID"] + "')");
               
            }
        }
    }
}
